package com.cw.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Vector;

import javax.annotation.Resource;

import com.cw.dao.GenericDao;
import com.cw.service.BaseService;

public abstract class BaseServiceImpl<T> implements BaseService<T> {

	private GenericDao<T> dao;

	/**
	 * @param dao
	 *            the dao to set
	 */
	@Resource
	public void setDao(GenericDao<T> dao) {
		this.dao = dao;
	}

	@Override
	public List<T> findAll() {
		// TODO Auto-generated method stub
		return dao.findAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hxrd.service.BaseService#batchHandlerByHQL(java.lang.String,
	 * java.lang.Object[])
	 */
	@Override
	public void batchHandlerByHQL(String sql, Object... objects) {
		dao.batchHandlerByHQL(sql, objects);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hxrd.service.BaseService#deleteEntity(java.lang.Object)
	 */
	@Override
	public void deleteEntity(T entity) {
		dao.deleteEntity(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hxrd.service.BaseService#findEntityBySql(java.lang.String,
	 * java.lang.Object[])
	 */
	@Override
	public List<T> findEntityBySql(String sql, Object... objects) {
		// TODO Auto-generated method stub
		return dao.findEntityBySql(sql, objects);
	}

	@Override
	public List<Object> findPropBySql(String sql, Object... objects) {
		// TODO Auto-generated method stub
		return dao.findPropBySql(sql, objects);
	}

	@Override
	public List<Object> findPropBySql(String sql, Integer fstRec, Integer maxRec, Object...objects) {
		// TODO Auto-generated method stub
		return dao.findPropBySql(sql, fstRec, maxRec, objects);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hxrd.service.BaseService#getEntity(java.io.Serializable)
	 */
	@Override
	public T getEntity(Serializable id) {
		// TODO Auto-generated method stub
		return dao.getEntity(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hxrd.service.BaseService#loadEntity(java.io.Serializable)
	 */
	@Override
	public T loadEntity(Serializable id) {
		// TODO Auto-generated method stub
		return dao.loadEntity(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hxrd.service.BaseService#saveEntity(java.lang.Object)
	 */
	@Override
	public void saveEntity(T entity) {
		dao.saveEntity(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hxrd.service.BaseService#saveOrUpdateEntity(java.lang.Object)
	 */
	@Override
	public void saveOrUpdateEntity(T entity) {
		dao.saveOrUpdateEntity(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hxrd.service.BaseService#updateEntity(java.lang.Object)
	 */
	@Override
	public void updateEntity(T entity) {
		dao.updateEntity(entity);
	}

	@Override
	public void deleteByNames(String tableName, String colName,
			Vector<String> rIds) {
		String hql = "delete from " + tableName + " where";
		int size = rIds.size();
		for (int i = 0; i < size; i++) {
			hql += " " + colName + "='" + rIds.get(i) + "'";
			if (i < size - 1) {
				hql += " or";
			}
		}
		dao.batchHandlerByHQL(hql);
	}

	@Override
	public void deleteByIds(String tableName, Vector<Integer> rIds) {
		String hql = "delete from " + tableName + " where id=?";
		int size = rIds.size();
		for (int i : rIds) {
			dao.batchHandlerByHQL(hql, i);
		}
	}

	@Override
	public void deleteByVector(String tableName, Vector<T> objs) {
		for (T t : objs) {
			dao.deleteEntity(t);
		}
	}

}
